package org.example.t06;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 死锁程序，观察Jstack输出日志。
 */
public class DeadLockThread {

    Object lock1 = new Object();
    Object lock2 = new Object();
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        DeadLockThread deadLock = new DeadLockThread();
        new Thread(()->{
            synchronized (deadLock.lock1){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (deadLock.lock2){
                    System.out.println("线程1拿到锁了");
                }
            }
        },"线程1").start();
        synchronized (deadLock.lock2){
            Thread.sleep(1000L);
            synchronized (deadLock.lock1){
                System.out.println("主线程拿到锁了");
            }
        }
    }
}
